package main

import (
	"container/heap"
	"fmt"
)

type IntHeap [][2]int

func (h IntHeap) Len() int {
	return len(h)
}

func (h IntHeap) Less(i, j int) bool {
	return h[i][1] < h[j][1]
}

func (h IntHeap) Swap(i, j int) {
	h[i], h[j] = h[j], h[i]
}

func (h *IntHeap) Push(x interface{}) {
	*h = append(*h, x.([2]int))
}

func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0:n-1]
	return x
}


func main()  {
	k := 2
	nums := []int{1,1,1,1,2,2,3}


	hashMap := map[int]int{}
	res := make([]int, k)
	for _, i := range nums {
		hashMap[i]++
	}
	h := &IntHeap{}
	heap.Init(h)

	fmt.Println(hashMap)
	for key, value := range hashMap {
		heap.Push(h, [2]int{key, value})
		if h.Len() > k {
			heap.Pop(h)
		}
	}

	for i:=0; i<k; i++ {
		res[k-i-1] = heap.Pop(h).([2]int)[0]
	}
	fmt.Println(res)

	//h := &IntHeap{2, 1, 5, 100, 3, 6, 5, 6}
	//heap.Init(h)
	//heap.Push(h, 90)
	//for h.Len() > 0 {
	//	fmt.Printf("%d ", heap.Pop(h))
	//}
}
